<?php

namespace common\models\project;

use Yii;
use yii\behaviors\BlameableBehavior;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;

/**
 * This is the model class for table "hxdc_project_article_album".
 *
 * @property int $id
 * @property int $merchant_id 商户id
 * @property int $article_id 关联article表id
 * @property string $typename 类型名
 * @property string $img_type 户型图;实景图...
 * @property string $note1
 * @property string $note2
 * @property string $img_url 图片链接
 * @property string $delimg
 * @property string $vr_url
 * @property string $fileId 腾讯云点播fileId
 * @property string $video_url 视频链接
 * @property string $height
 * @property string $width
 * @property int $sort 权重
 * @property int $created_at 创建时间
 * @property int $updated_at 最后一次更新时间
 * @property string $status 状态[-1:删除;0:禁用;1启用]
 */
class ProjectArticleAlbum extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'hxdc_project_article_album';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['merchant_id', 'article_id', 'sort', 'created_at', 'updated_at'], 'integer'],
            [['typename', 'height', 'width'], 'string', 'max' => 15],
            [['img_type'], 'string', 'max' => 2],
            [['note1', 'note2'], 'string', 'max' => 300],
            [['img_url'], 'string', 'max' => 127],
            [['delimg', 'vr_url', 'video_url'], 'string', 'max' => 255],
            [['fileId'], 'string', 'max' => 63],
            [['status'], 'string', 'max' => 4],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'merchant_id' => 'Merchant ID',
            'article_id' => 'Article ID',
            'typename' => 'Typename',
            'img_type' => 'Img Type',
            'note1' => 'Note1',
            'note2' => 'Note2',
            'img_url' => 'Img Url',
            'delimg' => 'Delimg',
            'vr_url' => 'Vr Url',
            'fileId' => 'File ID',
            'video_url' => 'Video Url',
            'height' => 'Height',
            'width' => 'Width',
            'sort' => 'sort',
            'created_at' => 'Created At',
            'updated_at' => 'Updated At',
            'status' => 'Status',
        ];
    }
    /**
     * @param bool $insert
     * @return bool
     * @throws \yii\base\Exception
     */
    public function beforeSave($insert)
    {
        if ($this->isNewRecord) {
            $this->created_at = time();
        }

        return parent::beforeSave($insert);
    }
    /**
     * @return array
     */
    public function behaviors()
    {
        $merchant_id = Yii::$app->services->merchant->getId();
        return [
            [
                'class' => TimestampBehavior::class,
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
                ],
            ],
            [
                'class' => BlameableBehavior::class,
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['merchant_id'],
                ],
                'value' => !empty($merchant_id) ? $merchant_id : 0,
            ]
        ];
    }
}
